<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="dialogStatus"
      :visible.sync="dialogEditFormVisible"
    >
      <el-form
        ref="dataForm"
        class="dialog-Backgroundcolor"
        :model="listQuery"
        label-position="right"
        label-width="100px"
      >
        <el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开户银行：">
                <el-select
                  v-model="listQuery.salaCardBank"
                  class="filter-item"
                  clearable
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in agencyBankLists"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="后续操作：">
                <el-select
                  v-model="listQuery.isLongUse"
                  class="filter-item"
                  clearable
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in options2"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col
              :span="12"
              style="margin-top:10px"
            >
              <el-form-item label="身份证号码：">
                <el-input
                  v-model="listQuery.idenNo"
                  class="filter-item Borderwidth"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-button
            type="primary"
            style="float:right;"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :height="tableHeight"
      style="width: 100%;"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        fixed
        prop="no"
        label="序号"
        align="center"
        width="55"
      />
      <el-table-column
        fixed
        label="单位名称"
        width="180"
        align="center"
      >
        <template slot-scope="{row}">
          <span style="float: left">{{ row.agencyName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed
        prop="personName"
        label="姓名"
        width="100"
        align="center"
      />
      <el-table-column
        fixed
        label="开户银行"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.salaCardBank | bankFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed
        prop="newSalaCardNo"
        label="新工资卡号"
        align="center"
        width="165"
      />
      <el-table-column
        fixed
        label="后续操作"
        align="center"
        width="80"
      >
        <template slot-scope="{row}">
          <span>{{ row.isLongUse | longUseFilter(that) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="idenNo"
        label="身份证号码"
        align="center"
        width="165"
      >
        <template slot-scope="{row}">
           <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '0' || tabPane ==='1'"
        label="实发工资总额"
        align="center"
      >
        <template slot-scope="{row}">
          <span style="float: right">{{ Number(row.sfgzze).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '0' || tabPane ==='1'"
        label="应发工资总额"
        align="center"
      >
        <template slot-scope="{row}">
          <span style="float: right">{{ Number(row.yfgzze).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="性别"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.sexCode | sexFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="民族"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.natCode | natFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="人员状态"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.state | personFlagFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="学历"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.schRecCode | schrecFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="职务"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.posCode | posgrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        label="职级"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.grCode | grFilter(that) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        v-if="tabPane === '2'"-->
      <!--        prop="isAuth"-->
      <!--        label="是否在编"-->
      <!--        align="center"-->
      <!--        width="100"-->
      <!--      />-->
      <el-table-column
        v-if="tabPane === '2'"
        label="工资级别"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.salaGrCode | salagrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane === '2'"
        prop="birthday"
        label="出生日期"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="enterAgencyDate"
        label="入职时间"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="retireDate"
        label="离退休时间"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="workInitDate"
        label="参加工作时间"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="endDate"
        label="调离时间"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="deadDate"
        label="死亡时间"
        align="center"
        width="140"
      />
      <el-table-column
        v-if="tabPane === '2'"
        prop="remarks"
        label="备注"
        align="center"
        width="70"
      />

    </el-table>
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getSex,
  getNat,
  getSchrec,
  getPosgr,
  getGr,
  getSalagr,
  getAgencyBank,
  exportSalaryData
} from '@/api/baseInfo'
import {
  queryCardNumberFiscalPage,
  updatePersonnelCardNumberFiscal,
  undoUpdatePersonnelCardNumberFiscal,
  generateBankData,
  regenerateBankData
} from '@/api/exceptionCardNumberManage/cardNumberFinancialProcess'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'

export default {
  name: 'Unit',
  components: { Pagination, tableEmpty },
  filters: {
    // 性别过滤器
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
    },
    // 民族过滤器
    natFilter(type, that) {
      const natLists = that.natLists
      for (const item in natLists) {
        if (natLists[item].code === type) {
          return natLists[item].name
        }
      }
    },
    // 学历过滤器
    schrecFilter(type, that) {
      const schrecLists = that.schrecLists
      for (const item in schrecLists) {
        if (schrecLists[item].code === type) {
          return schrecLists[item].name
        }
      }
    },
    // 职务过滤器
    posgrFilter(type, that) {
      const posgrLists = that.posgrLists
      for (const item in posgrLists) {
        if (posgrLists[item].code === type) {
          return posgrLists[item].name
        }
      }
    },
    // 职级过滤器
    grFilter(type, that) {
      const grLists = that.grLists
      for (const item in grLists) {
        if (grLists[item].code === type) {
          return grLists[item].name
        }
      }
    },
    // 工资级别过滤器
    salagrFilter(type, that) {
      const salagrLists = that.salagrLists
      for (const item in salagrLists) {
        if (salagrLists[item].code === type) {
          return salagrLists[item].name
        }
      }
    },
    // 人员状态过滤器
    personFlagFilter(type, that) {
      if (type === '1') {
        return '离退休'
      } else if (type === '3') {
        return '在职'
      }
    },
    bankFilter(type, that) {
      const agencyBankLists = that.agencyBankLists
      for (const item in agencyBankLists) {
        if (agencyBankLists[item].code === type) {
          return agencyBankLists[item].name
        }
      }
      return ''
    },
    longUseFilter(type, that) {
      if (type === 1) {
        return '长期使用'
      } else if (type === 0) {
        return '本月使用'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined&& val!== null){//避免列表中存在空数据报错
        return `${val.substring(0,4)}****${val.substring(val.length-4)}`
      }else{
        return val
      }
    }
  },
  props: ['tabPane'],
  data() {
    return {
      tableHeight: null,
      dialogStatus: '',
      listLoading: true,
      dialogEditFormVisible: false,
      that: this,
      listQuery: {
        page: 1,
        limit: 200,
        flag: 0,
        agencyId: '0',
        idenNo: '',
        salaCardBank: '',
        isLongUse: ''
      },
      list: [],
      listUpdate: [],
      total: 0,
      sexLists: [], // 性别
      natLists: [], // 民族
      schrecLists: [], // 学历
      posgrLists: [], // 职务
      grLists: [], // 职级
      salagrLists: [], // 工资级别
      agencyBankLists: [], // 银行
      listUnit: [],

      options2: [
        {
          value1: ' ',
          label: '全部'
        },
        {
          code: '0',
          name: '本月使用'
        },
        {
          code: '1',
          name: '长期使用'
        }
      ],
      value1: ''
    }
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  // 开始的地方
  created() {
    this.listQuery.page = 1
    this.listQuery.flag = this.tabPane
    this.initBaseInfo()
    this.getList()
  },
  methods: {
    // 树方法
    handleTree(data) {
      // if (!Object.prototype.hasOwnProperty.call(data, 'children')) {
      this.listQuery.agencyId = data.id
      this.getList()
      // }
    },
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    getTbHeight() {
      this.tableHeight = window.innerHeight - 220
    },
    getList() {
      this.listLoading = true
      queryCardNumberFiscalPage(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 查询
    query() {
      this.dialogStatus = '查询'
      this.dialogEditFormVisible = true
    },

    handleQuery() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
      this.dialogEditFormVisible = false
    },
    // 初始化基础数据
    initBaseInfo() {
      getSex().then(response => {
        this.sexLists = Object.assign({}, response.data)
      })
      getNat().then(response => {
        this.natLists = Object.assign({}, response.data)
      })
      getSchrec().then(response => {
        this.schrecLists = Object.assign({}, response.data)
      })
      getPosgr().then(response => {
        this.posgrLists = Object.assign({}, response.data)
      })
      getGr().then(response => {
        this.grLists = Object.assign({}, response.data)
      })
      getSalagr().then(response => {
        this.salagrLists = Object.assign({}, response.data)
      })
      getAgencyBank().then(response => {
        this.agencyBankLists = Object.assign({}, response.data)
      })
    },
    // 更新人事信息
    updatePersonnel() {
      if (this.list.length === 0) {
        this.$message({
          showClose: true,
          message: '暂无数据，不能更新',
          type: 'warning',
          duration: 4000
        })
        this.$emit('PersonnelCallBack')
        return
      }

      // 提示框确认
      // this.$confirm('确定更新人事信息吗？', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      updatePersonnelCardNumberFiscal(this.listQuery).then(response => {
        this.$message({
          showClose: true,
          message: '更新人事信息成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('PersonnelCallBack')
        // 更新
        this.handleQuery()
        // })
      }).catch(() => {
        this.$emit('PersonnelCallBack')
      })
    },
    // 撤销更新人事信息
    undoUpdatePersonnel() {
      if (this.list.length === 0) {
        this.$message({
          showClose: true,
          message: '暂无数据，不能撤销',
          type: 'warning',
          duration: 4000
        })
        this.$emit('UpdatePersonnelCallBack')
        return
      }
      // 提示框确认
      // this.$confirm('确定撤销跟新人事信息吗？', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      undoUpdatePersonnelCardNumberFiscal(this.listQuery).then(response => {
        this.$message({
          showClose: true,
          message: '撤销更新人事信息成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('UpdatePersonnelCallBack')
        // 更新
        this.handleQuery()
        // })
      })
        .catch(() => {
          this.$emit('UpdatePersonnelCallBack')
        })
    },
    //  生成银行数据
    generateBankData() {
      generateBankData().then(() => {
        this.$message({
          showClose: true,
          message: '生成并上传成功',
          type: 'success',
          duration: 4000
        })
        this.getList()
        this.$emit('generateBankDataCallBack')
      }).catch(() => {
        this.$emit('generateBankDataCallBack')
      })
    },

    //  重新生成银行数据
    regenerateBankData() {
      regenerateBankData().then(() => {
        this.$message({
          showClose: true,
          message: '生成并上传成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('generateBankDataCallBack')
      }).catch(() => {
        this.$emit('regenerateBankDataCallBack')
      })
    },
    exportSalaryData() {
      const province = this.$store.getters.userInfo.province
      const fileName = 'exceptionCardNumberFile'
      exportSalaryData(fileName, province).then((response) => {
        const link = document.createElement('a')
        const objectUrl = URL.createObjectURL(response)
        link.href = objectUrl
        link.download = '银行数据.zip'
        link.click()
        URL.revokeObjectURL(objectUrl)
        this.$emit('exportCallback')
      }).catch(() => {
        this.$emit('exportCallback')
      })
    }

  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/border.scss"; /*引入公共样式*/
.el-col-5 {
  width: 50%;
}

::v-deep.el-table--scrollable-x {
  .el-table__body-wrapper {
    height: 414px !important;
  }
}
</style>
